package com.itheima.cs.dao.dbd;

import com.itheima.cs.entity.dbd.InternalTransfer;
import com.itheima.cs.help.DbOpenHelper;
import java.sql.*;

import java.sql.SQLException;


public class InternalTransferDao extends DbOpenHelper {

    public Integer createTransferAndReturnId(InternalTransfer transfer) {
        try {
            getConnection();
            String sql = "INSERT INTO dbd (transfer_number, doc_type, out_warehouse_no, " +
                    "out_warehouse_name, in_warehouse_no, in_warehouse_name, material_code, " +
                    "material_name, material_spec, quantity, date, user_id, dbd_uuid) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";

            pStmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pStmt.setString(1, "TBD");
            pStmt.setString(2, transfer.getDocType());
            pStmt.setString(3, transfer.getOutWarehouseNo());
            pStmt.setString(4, transfer.getOutWarehouseName());
            pStmt.setString(5, transfer.getInWarehouseNo());
            pStmt.setString(6, transfer.getInWarehouseName());
            pStmt.setString(7, transfer.getMaterialCode());
            pStmt.setString(8, transfer.getMaterialName());
            pStmt.setString(9, transfer.getMaterialSpec());
            pStmt.setString(10,transfer.getEtQuantity());
            pStmt.setString(11, transfer.getDate());
            pStmt.setString(12, transfer.getUserId());
            pStmt.setString(13, transfer.getDbdUUID());
            pStmt.executeUpdate();

            try (ResultSet rs = pStmt.getGeneratedKeys()) {
                if (rs.next()) return rs.getInt(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return null;
    }

    public boolean updateTransferInfo(int localId, String number, int erpId, int reqType) {
        try {
            getConnection();
            pStmt = conn.prepareStatement(
                    "UPDATE dbd SET transfer_number=?, transfer_id=?, dbd_status=? WHERE id=?"
            );
            pStmt.setString(1, number);
            pStmt.setInt(2, erpId);
            pStmt.setInt(3, reqType);   //reqType:表单状态：0：表单创建；1：表单提交
            pStmt.setInt(4, localId);
            return pStmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            closeAll();
        }
    }

    public InternalTransfer getTransferById(int id) {
        try {
            getConnection();
            String sql = "SELECT * FROM dbd WHERE id = ?";
            pStmt = conn.prepareStatement(sql);
            pStmt.setInt(1, id);
            ResultSet rs = pStmt.executeQuery();

            if (rs.next()) {
                InternalTransfer transfer = new InternalTransfer();
                transfer.setTransferNumber(rs.getString("transfer_number"));
                transfer.setOutWarehouseName(rs.getString("out_warehouse_name"));
                transfer.setInWarehouseName(rs.getString("in_warehouse_name"));
                transfer.setMaterialCode(rs.getString("material_code"));
                transfer.setMaterialName(rs.getString("material_name"));
                transfer.setMaterialSpec(rs.getString("material_spec"));
                transfer.setEtQuantity(rs.getString("quantity"));
                transfer.setDate(rs.getString("date"));
                transfer.setInWarehouseNo(rs.getString("in_warehouse_no"));
                transfer.setUserId(rs.getString("user_id"));
                return transfer;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }
        return null;
    }
}
